<?php
namespace Admin\Controller;

use Admin\Util\lib\YZTokenClient;
use Util\AccessController;
use Admin\Util\lib\YouZanApiFunction;

class RegionController extends AccessController {

    /*
     * 由id获取书籍信息
     */
    public function Index(){
        $region_fid = $_GET['region_fid'] ? $_GET['region_fid'] : '0';
        $region_fname = $_GET['region_fname'] ? $_GET['region_fname'] :  '字典树管理';
        $LS = Service('Region','region');
        $where['region_fid'] = $region_fid;
        $DATA = $LS->query($where);
        $this->DATA = $DATA;
        $this->region_fid = $region_fid;
        $this->region_fname = $region_fname;
        $this->display();
    }

    public function ReturnP(){
        $region_fid = $_GET['region_fid'] ;
        $LS = Service('Region','region');
        $where['region_id'] = $region_fid;
        $DATA = $LS->find($where);
        $this->redirect('Region/Index',array('region_fid'=>$DATA['region_fid']));
    }

    public function Add(){
        $LS = Service('Region','region');
        if($_POST['region_id']){
            $where['region_id'] = $_POST['region_id'];
            $save['region_name'] = $_POST['region_name'];
            $save['region_code'] = $_POST['region_code'];
            $OK = $LS->update($where,$save);
        }else{
            $save = $_POST;
            $save['region_id'] = 'ZD'.uniqid().rand(0,9);
            $OK = $LS->insert($save);
        }
        $OK ? $this->success('添加/编辑成功'):$this->error('添加/编辑失败');
    }
    public function Del() {
        $where['region_id'] = $_GET['region_id'];
        $LS = Service('Region','region');
        $OK = $LS->remove($where);
        $OK ? $this->success('删除成功'):$this->error('删除失败');
    }
}